//
//  ViewController.swift
//  RX
//
//  Created by iOS开发 on 2019/2/11.
//  Copyright © 2019 iOS开发. All rights reserved.
//

import UIKit
import RxSwift

class ViewController: UIViewController {

    enum condition : String {
        case wifi = "Wifi"
        case none = "none"
        case celluar = "celluar"
    }
    
    public func delay(_ delay: Double, closure: @escaping()->Void){
        DispatchQueue.main.asyncAfter(deadline: .now()+delay) {
            closure()
        }
    }
    
    public func example(_ desp: String, action: ()->Void) {
        
        print("========\(desp)========")
        action()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
//        let eventNumber = Observable.from(["1", "2", "3", "4", "5", "6", "7", "8", "9"])
//            .map{Int($0)}
//            .filter{
//                if let item = $0 , item % 2 == 0 {
//                    return true
//                }
//                return false
//        }
        
//        eventNumber.subscribe { (event) in
//            print(event)
//        }
//        eventNumber.subscribe(onNext: { (event) in
//            print(event ?? "")
//        }, onError: {
//            print($0)
//        }, onCompleted: {
//            print("completed1")
//        }) {
//
//        }
        
//        var bag = DisposeBag()
//        Observable<Int>.interval(1, scheduler: MainScheduler.instance).subscribe(
//            onNext:{print($0)},
//            onDisposed: {print("回收")}
//            ).disposed(by:bag)
//
//        delay(5) {
//            bag = DisposeBag()
//        }
        
//        enum CustomErr : Error {
//            case somethingError
//        }
//
//        let customOb = Observable<Int>.create { (observer) -> Disposable in
//
//            observer.onNext(10)
////            observer.onError(CustomErr.somethingError)
//
//            observer.onCompleted()
//            return Disposables.create()
//        }
//
//        let bag = DisposeBag()
//        customOb.debug().subscribe(
//            onNext: {print($0)},
//            onError: {print($0)},
//            onCompleted: {print("完成")},
//            onDisposed: {print("over")}
//            ).disposed(by: bag)
        
        
//        let arr = Variable<[String]>([])
//
//        arr.value.append("1")
//
//        arr.asObservable().subscribe { (event) in
//            print(event)
//        }
//
//        arr.value.append("2")
        
//        example("ignoreElements") {
//            let task = PublishSubject<String>()
//            let bag = DisposeBag()
//
//            task.ignoreElements().subscribe {print($0)}.disposed(by: bag)
//
//            task.onNext("T1")
//            task.onNext("T2")
//            task.onNext("T3")
//            task.onCompleted()
//        }
        
//        example("skip") {
//            let task = PublishSubject<String>()
//            let bag = DisposeBag()
//
//            task.skip(3).subscribe {print($0)}.disposed(by: bag)
//
//            task.onNext("T1")
//            task.onNext("T2")
//            task.onNext("T3")
//            task.onCompleted()
//        }
        
//        example("skipWhile") {
//            let task = PublishSubject<String>()
//            let bag = DisposeBag()
//
//            task.skipWhile {$0 != "T2"} .subscribe {print($0)}.disposed(by: bag)
//
//            task.onNext("T1")
//            task.onNext("T2")
//            task.onNext("T3")
//            task.onCompleted()
//
//        }
        
//        example("skipUntil") {
//            let task = PublishSubject<String>()
//            let bag = DisposeBag()
//            let boolObservable = PublishSubject<Void>()
//
//            task.skipUntil(boolObservable).subscribe {print($0)}.disposed(by: bag)
//
//            task.onNext("T1")
//            task.onNext("T2")
//            boolObservable.onNext(())
//            task.onNext("T3")
//            task.onCompleted()
//
//        }
        
//        example("elementAt") {
//            let task = PublishSubject<String>()
//            let bag = DisposeBag()
//
////            task.elementAt(2).subscribe{print($0)}.disposed(by: bag)
//            task.filter {$0 == "T2"}.subscribe{print($0)}.disposed(by: bag)
//            task.onNext("T1")
//            task.onNext("T2")
//            task.onNext("T3")
//            task.onCompleted()
//        }
        
//        example("take") {
//            let task = PublishSubject<String>()
//            let bag = DisposeBag()
//
//            task.takeWhileWithIndex{ (value,index) in
//                value != "T2"
//            }.subscribe{print($0)}.disposed(by: bag)
//            task.onNext("T1")
//            task.onNext("T2")
//            task.onNext("T3")
//            task.onCompleted()
//        }
        
//        let bag = DisposeBag()
//        Observable.of(1,2,3).toArray().subscribe(onNext: {
//            dump(type(of: $0))
//            dump($0)
//        }).disposed(by: bag)
        
//        Observable.of(1,2,3)
//            .scan(0, accumulator: +).subscribe({
//            dump($0)
//        }).disposed(by: bag)
        
//        Observable.of(1,2,3)
//            .map({ value in
//                value * 2
//            }).subscribe({
//                dump($0)
//            }).disposed(by: bag)
        

        let bag = DisposeBag()
        
//        let request = Observable<String>.create{
//            $0.onNext("response from the server")
//            $0.onCompleted()
//            return Disposables.create()
//        }
//        request.startWith(condition.celluar.rawValue).subscribe (onNext : {dump($0)}).disposed(by: bag)

        
        let queueA = PublishSubject<String>()
        let queueB = PublishSubject<String>()
        
        Observable.concat([queueA,queueB]).subscribe {
            dump($0)
        }.disposed(by: bag)
        
        queueA.onNext("A")
        queueA.onNext("B")
        queueA.onNext("C")
        queueA.onCompleted()
        queueB.onNext("A")
        
        
        let button = UIButton(type: .custom)
        button.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
        button.backgroundColor = UIColor.red
        self.view.addSubview(button)
        button.addTarget(self, action: #selector(pushTableView), for: UIControl.Event.touchUpInside)
     }
    
    @objc func pushTableView() -> Void {
        self.present(TableViewController(), animated: true, completion: nil)
    }
    
    
 
}

